﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Module.Interfaces.Common;
using Microsoft.Practices.Unity;
using Module.MultiSiteFields.Controllers;
using Module.MultiSiteFields.Interfaces;
using Core.Module.Interfaces.Controller;
using Module.MultiSiteFields.Managers;
using Core.Module.Interfaces.Manager;
using Core.Module.Interfaces;
using Core.Module.Manager;
using Core.Module.Controller;
using System.Web.Script.Serialization;
using Core.Module.Interfaces.Model;
using Core.Module.Model;

namespace Module.MultiSiteFields
{
    public class TypeRegistrator:ITypeRegistrator
    {
        public IUnityContainer RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<JavaScriptConverter, ModuleNodeJavaScriptConverter>("nodeconverter", new ContainerControlledLifetimeManager());

            container.RegisterType<TreeStartPoint, TreeStartPoint>(new ContainerControlledLifetimeManager());
            container.RegisterType<IStartPointsController, TreeSettingsController>(new ContainerControlledLifetimeManager());
            container.RegisterType<INodeManager<IBackOffice>, NodeManagerBackOffice>("content", new ContainerControlledLifetimeManager());
            container.RegisterType<INodeController<IBackOffice>, NodeControllerBackOffice>("content", new ContainerControlledLifetimeManager());
            container.RegisterType<INodeManager<IBackOffice>, NodeManagerBackOffice>("media", new ContainerControlledLifetimeManager());
            container.RegisterType<INodeController<IBackOffice>, NodeControllerBackOffice>("media", new ContainerControlledLifetimeManager());            
            //container.RegisterType<INodeManager<IBackOffice>, MediaNodeManager>("media", new ContainerControlledLifetimeManager());
            //container.RegisterType<INodeController<IBackOffice>, MediaController>("media", new ContainerControlledLifetimeManager());
            container.RegisterType<INodeManager<IBackOffice>, NodeManagerBackOffice>("member", new ContainerControlledLifetimeManager());
            container.RegisterType<INodeController<IBackOffice>, NodeControllerBackOffice>("member", new ContainerControlledLifetimeManager());
            
            return container;
        }
    }

    public class ModuleNodeJavaScriptConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            IModuleNode result = ObjectManager.Resolve<IModuleNode>();
            result.Alias = dictionary["alias"] as string;
            result.Id = dictionary["id"] as string;
            result.Name= dictionary["name"] as string;
            result.Parents = dictionary["parents"] as string[];
            result.Path = dictionary["path"] as string;
            result.SiteId= dictionary["siteid"] as string;
            result.Title= dictionary["title"] as string;
            result.Url= dictionary["url"] as string;
            result.Icon=dictionary["icon"] as string;
            return result;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            
            Dictionary<string, object> result = new Dictionary<string, object>();
            IModuleNode node = obj as IModuleNode;
            if (obj == null || node==null) return result;
            result["alias"] = node.Alias;
            result["id"] = node.Id;
            result["name"] = node.Name;
            result["parents"] = node.Parents;
            result["path"] = node.Path;
            result["siteid"] = node.SiteId;
            result["title"] = node.Title;
            result["url"] = node.Url;
            result["icon"] = node.Icon;
            return result;

        }

        public override IEnumerable<Type> SupportedTypes
        {
            get {
                return new List<Type>() { typeof(IModuleNode), typeof(ModuleNode) };
            }
        }
    }
}
